home *** CD-ROM | disk | FTP | other *** search
- /*
- File: UseRsrcM.cpp
-
- Contains: Functions for using resources from the resource fork of a library
- and others for loading resources into temp mem.
-
- Owned by: Reggie Adkins
-
- Copyright: © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
-
- <3> 10/22/96 DH 1369514 Overhauled resource loading
- functions so that a variety of places can
- be searched for resources to load into temp
- mem.
-
- <2> 6/21/96 DH 1353507: 1.0.x: Dragging Scrapbook OpenDoc
- clipping to document can cause crash. Added
- function to read a resource fork that takes
- a fileRefNum
- */
-
-
- #ifndef _USERSRCM_
- #include "UseRsrcM.h"
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef _ODDEBUG_
- #include "ODDebug.h"
- #endif
-
- #ifndef _ODMEMORY_
- #include <ODMemory.h>
- #endif
-
- #ifndef _PASCLSTR_
- #include <PasclStr.h>
- #endif
-
- #ifndef __FRAGRSRC__
- #include <FragRsrc.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
-
- static CFragResFileRef gFragRef = kODNULL; // Reference to resource map
- static short gRefNum = -1; // RefNum of resource file
- static ODSLong gNesting = 0; // Nesting level (>0 means in use)
-
-
- OSErr
- InitLibraryResources( CFragInitBlockPtr init )
- {
- return InitCFragResources(init,&gFragRef);
- }
-
-
- void
- CloseLibraryResources( )
- {
- if( gNesting>0 )
- WARN("Resource file still in use while being closed!");
- OSErr err= TermCFragResources(gFragRef);
- if( err )
- WARN("TermCFragResources returned %hd",err);
-
- gFragRef = kODNULL;
- gRefNum = -1;
- gNesting = 0;
- }
-
-
- ODSLong BeginUsingLibraryResources( )
- {
- short ref = CurResFile();
- if( gNesting > 0 )
- UseResFile(gRefNum);
- else {
- THROW_IF_ERROR( BeginCFragResources(gFragRef) );
- gRefNum = CurResFile();
- }
- gNesting++;
- return ref;
- }
-
-
- void EndUsingLibraryResources( ODSLong ref )
- {
- if( gNesting > 0 )
- if( --gNesting == 0 )
- THROW_IF_ERROR( EndCFragResources(gFragRef) );
- UseResFile(ref);
- }
-
-
- CUsingLibraryResources::~CUsingLibraryResources( )
- {
- ODSLong ref = fRefNum;
- if( ref != -1 ) { // Make sure it's done only once
- fRefNum = -1;
- EndUsingLibraryResources(ref);
- }
- }
-
-
- // RESOURCE UTILITIES:
-
- //------------------------------------------------------------------------------
- // BasicReadResource
- //
- // This function loads the resource out of the part's library file or the OpenDocLib.
- // The advantage of using this function over the Mac Resource Mgr directly is
- // that this will load the resource into temp mem rather than the app heap.
- //------------------------------------------------------------------------------
-
- static void*
- BasicReadResource( ResType type, short id, ConstStr255Param name, ODBoolean asHandle )
- {
- void *result = kODNULL; ODVolatile(result);
-
- ODSLong savedRef = BeginUsingLibraryResources();
-
- TRY{
- result = ReadResourceIntoTempMem( type, id, name, asHandle );
- }CATCH_ALL{
- EndUsingLibraryResources(savedRef);
- RERAISE;
- }ENDTRY
-
- EndUsingLibraryResources(savedRef);
- return result;
- }
-
-
- //------------------------------------------------------------------------------
- // The following few utility functions will load resources into temp mem rather than
- // the app heap. The only limitation is that they will only load resources from your
- // part libray or the OpenDoc library. To load resources from arbitrary files, see the
- // functions further down.
- //------------------------------------------------------------------------------
-
- ODHandle
- ODReadResource( ResType type, short id )
- {
- return (ODHandle) BasicReadResource(type,id,kODNULL,kODTrue);
- }
-
- ODHandle
- ODReadNamedResource( ResType type, ConstStr255Param name )
- {
- if( name==kODNULL )
- THROW(kODErrIllegalNullInput);
- return (ODHandle) BasicReadResource(type,0,name,kODTrue);
- }
-
- ODPtr
- ODReadResourceToPtr( ResType type, short id )
- {
- return BasicReadResource(type,id,kODNULL,kODFalse);
- }
-
- ODPtr
- ODReadNamedResourceToPtr( ResType type, ConstStr255Param name )
- {
- if( name==kODNULL )
- THROW(kODErrIllegalNullInput);
- return BasicReadResource(type,0,name,kODFalse);
- }
-
-
- void
- ODGetString( Str255 str, short id )
- {
- str[0] = 0;
- ODSLong savedRef = BeginUsingLibraryResources();
- Handle h = Get1Resource('STR ',id);
- OSErr result = ResError();
- if( h ) {
- CopyPascalString(str,(StringPtr)*h);
- ReleaseResource(h);
- }
- EndUsingLibraryResources(savedRef);
- if( !h ) {
- THROW(result);
- THROW(resNotFound);
- }
- }
-
-
- void
- ODGetIndString( Str255 str, short id, short index )
- {
- str[0] = 0;
- ODSLong savedRef = BeginUsingLibraryResources();
- Handle h = Get1Resource('STR#',id);
- OSErr result = ResError();
- if( h ) {
- #if ODDebug
- if( index<1 || index>**(short**)h ) {
- WARN("ODGetIndString(%hd,%hd): invalid index",id,index);
- result= kODErrValueOutOfRange;
- } else
- #endif
- GetIndString(str,id,index);
- ReleaseResource(h);
- }
- EndUsingLibraryResources(savedRef);
- if( result ) {
- THROW(result);
- THROW(resNotFound);
- }
- }
-
- //------------------------------------------------------------------------------
- // ODReadResourceUsingRefnum
- //
- // This function takes a refnum to load the resource from. This is different than
- // the other resource loading functions because this one will only load the
- // resource from the resource fork pointed to by the refnum parameter, whereas
- // the original assumes it will load out of the part's library file or the OpenDocLib.
- // The advantage of using this function over the Mac Resource Mgr directly is
- // that this will load the resource into temp me rather than the app heap.
- //------------------------------------------------------------------------------
-
- void*
- ODReadResourceUsingRefnum( short ResRefNum, ResType type, short id, ConstStr255Param name, ODBoolean asHandle )
- {
- void *result = kODNULL; ODVolatile(result);
-
- short oldResRefnum; ODVolatile( oldResRefnum );
- oldResRefnum = CurResFile(); // save the current start of the resource map.
- UseResFile( ResRefNum );
-
- TRY{
- ReadResourceIntoTempMem( type, id, name, asHandle );
- }
- CATCH_ALL{
- UseResFile( oldResRefnum ); // restore the old start of the resource map
- RERAISE;
- }ENDTRY
-
- UseResFile( oldResRefnum ); // restore the old start of the resource map
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // ReadResourceIntoTempMem
- //
- // Yet another resource loading function. When called directly will search the
- // entire resource chain for the specified resource rather than in a particular
- // refnum or in a particular library. Useful for loading resources from a
- // collection of files.
- // The advantage of using this function over the Mac Resource Mgr directly is
- // that this will load the resource into temp me rather than the app heap.
- //------------------------------------------------------------------------------
-
- void* ReadResourceIntoTempMem( ResType type, short id, ConstStr255Param name, ODBoolean asHandle )
- {
- Handle rsrc; ODVolatile(rsrc);
- void *result = kODNULL; ODVolatile(result);
-
- SetResLoad(false);
- if( name )
- rsrc = Get1NamedResource(type,name);
- else
- rsrc = Get1Resource(type,id);
- SetResLoad(true);
- if( rsrc == kODNULL ) {
- OSErr err = ResError();
- THROW( err ? err : resNotFound );
- }
-
- TRY{
- ODSize size = (*rsrc) ?GetHandleSize(rsrc) :GetResourceSizeOnDisk(rsrc);
- void *dst;
- if( asHandle ) {
- result = (void*) ODNewHandle(size);
- dst = ODLockHandle((ODHandle)result);
- } else
- result = dst = ODNewPtr(size);
-
- if( *rsrc == kODNULL ) {
- // Resource is not in memory, use partial-resource call:
- ReadPartialResource( rsrc, 0, dst, size );
- THROW_IF_ERROR( ResError() );
- ReleaseResource(rsrc);
- } else {
- // Resource is already in memory; just copy it:
- ODBlockMove(*rsrc,dst,size);
- }
-
- if( asHandle )
- ODUnlockHandle((ODHandle)result);
-
- }CATCH_ALL{
- if( *rsrc == kODNULL )
- ReleaseResource(rsrc);
- if( result )
- if( asHandle )
- ODDisposeHandle((ODHandle)result);
- else
- ODDisposePtr(result);
- RERAISE;
- }ENDTRY
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // ReadIndexedResourceIntoTempMem
- //
- // Has the same functionality as Get1IndResource, where given an index i, this
- // function will load the i'th resource of the number 1 to n of resources, where
- // n is returned by Count1Resources. When called directly will search the
- // entire resource chain for the specified resource rather than in a particular
- // refnum or in a particular library. Useful for loading all resources of a
- // particular type.
- // The advantage of using this function over the Mac Resource Mgr directly is
- // that this will load the resource into temp me rather than the app heap.
- //------------------------------------------------------------------------------
-
- void* ReadIndexedResourceIntoTempMem( ResType type, short index, ODBoolean asHandle )
- {
- Handle rsrc; ODVolatile(rsrc);
- void *result = kODNULL; ODVolatile(result);
-
- SetResLoad(false);
- rsrc = Get1IndResource( type, index );
- SetResLoad(true);
- if( rsrc == kODNULL ) {
- OSErr err = ResError();
- THROW( err ? err : resNotFound );
- }
-
- TRY{
- ODSize size = (*rsrc) ?GetHandleSize(rsrc) :GetResourceSizeOnDisk(rsrc);
- void *dst;
- if( asHandle ) {
- result = (void*) ODNewHandle(size);
- dst = ODLockHandle((ODHandle)result);
- } else
- result = dst = ODNewPtr(size);
-
- if( *rsrc == kODNULL ) {
- // Resource is not in memory, use partial-resource call:
- ReadPartialResource( rsrc, 0, dst, size );
- THROW_IF_ERROR( ResError() );
- ReleaseResource(rsrc);
- } else {
- // Resource is already in memory; just copy it:
- ODBlockMove(*rsrc,dst,size);
- }
-
- if( asHandle )
- ODUnlockHandle((ODHandle)result);
-
- }CATCH_ALL{
- if( *rsrc == kODNULL )
- ReleaseResource(rsrc);
- if( result )
- if( asHandle )
- ODDisposeHandle((ODHandle)result);
- else
- ODDisposePtr(result);
- RERAISE;
- }ENDTRY
-
- return result;
- }
-